﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BLL.TcpUdp;
using BLL.Dataprogram;
using System.Threading;
using Framework.Kit;

namespace Client
{
    /// <summary>
    /// login.xaml 的交互逻辑
    ///</summary>
    public partial class login : Window
    {
        public login()
        {           
            InitializeComponent();           
            //窗体居中
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
        }

        /// <summary>
        /// 点击登录事件
        /// </summary>
        private void login_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //发送登录请求
                sendLogin();
            }
            catch (Exception ex)
            {
                other.WriteErrLog("点击登录事件", ex);
            }

            if (App.client.IsConnected)
            {
                System.Threading.Thread.Sleep(App.timespan);
                //如里已登录上
                if (App.islogin)
                {
                    //转到主客户显示页面
                    this.DialogResult = true;
                }
            }
        }


        /// <summary>
        /// 发送登录代码
        /// </summary>
        private void sendLogin()
        {
            //登录信息验证，不能为空且没有"|"字符
            if (username.Text.ToString().Length < 1 || password.Password.ToString().Length < 1 
                || username.Text.ToString().IndexOf("|") > 0 || password.Password.ToString().IndexOf("|") > 0)
            {
                MessageBox.Show("用户名或密码不能为空且不能含有'|'字符");
            }
            else
            {
                App.sendData(commandType.c003, username.Text.ToString() + "|" + password.Password.ToString());
            }
        }


        /// <summary>
        /// 窗体拖拉操作
        /// </summary>
        private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left) this.DragMove();
        }

        /// <summary>
        /// 关闭客户端
        /// </summary>
        private void closeBtn_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            Application.Current.Shutdown();
        }
    }
}
